package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.dto.system.HttpSettingDTO;
import com.jmb.api.model.entity.HttpSetting;
import com.jmb.api.service.HttpSettingService;
import com.jmb.api.mapper.HttpSettingMapper;
import com.jmb.api.vo.system.HttpSettingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author jinbo
* @description 针对表【http_setting】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class HttpSettingServiceImpl extends ServiceImpl<HttpSettingMapper, HttpSetting>
    implements HttpSettingService{

    @Autowired
    private HttpSettingMapper httpSettingMapper;
    @Override
    public void saveHtppSetting(HttpSettingDTO httpSettingDTO) {
        Integer type = httpSettingDTO.getType();
        if (type == 0 || type==3 || type==4){ // 若为代理/ConnectTimeout/ReadTimeout则将状态置为未启用
            httpSettingDTO.setStatusId(1);
        }else {
            httpSettingDTO.setStatusId(0);
        }
        this.save(httpSettingDTO);
    }

    @Override
    public IPage<HttpSettingDTO> getListHttpSetting(IPage<HttpSettingDTO> page, HttpSettingVo httpSettingVo) {
        return httpSettingMapper.selectHttpSettingList(page,httpSettingVo);
    }

    @Override
    public void updateHttpSetting(HttpSettingDTO httpSettingDTO) {
        LambdaUpdateWrapper<HttpSetting> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(HttpSetting::getSettingId,httpSettingDTO.getSettingId());
        BeanUtils.copyProperties(httpSettingDTO,updateWrapper);
        this.update(httpSettingDTO,updateWrapper);
    }

    @Override
    public void updateStatusId(Integer settingId, Integer statusId) {
        LambdaUpdateWrapper<HttpSetting> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(HttpSetting::getSettingId,settingId);
        updateWrapper.set(HttpSetting::getStatusId,statusId);
        this.update(updateWrapper);
    }

    @Override
    public List<HttpSettingDTO> getHttpSettingList(HttpSettingDTO httpSettingDTO) {
        LambdaQueryWrapper<HttpSetting> wrapper = new LambdaQueryWrapper<HttpSetting>()
                .eq(HttpSetting::getType, httpSettingDTO.getType())
                .eq(HttpSetting::getStatusId, httpSettingDTO.getStatusId());
        return httpSettingMapper.selectList(wrapper).stream().map(item->{
            HttpSettingDTO dto = new HttpSettingDTO();
            BeanUtils.copyProperties(item,dto);
            return dto;
        }).collect(Collectors.toList());
    }
}




